package com.donleo.cn.annotation.dao.impl;

import com.donleo.cn.annotation.Delete;
import com.donleo.cn.annotation.Insert;
import com.donleo.cn.annotation.Select;
import com.donleo.cn.annotation.Update;
import com.donleo.cn.annotation.dao.UserMapper;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;

/**
 * @author liangd
 * date 2020-11-17 11:28
 * code 方法动态代理工厂
 */
public class MethodProxyFactory implements UserMapper {
    @Override
    public void add(Integer id, String username, Integer age, String phone, String email) {
        //利用反射获取userMapper所有方法
        //1、通过类对象
        Method[] methods = UserMapper.class.getDeclaredMethods();
        for (Method method : methods) {
            //判断方法注解是否是指定注解
            if (method.isAnnotationPresent(Insert.class)) {
                String sql = method.getAnnotation(Insert.class).value();
                //替换?，只替换一次
                sql = StringUtils.replaceOnce(sql, "?", String.valueOf(id));
                sql = StringUtils.replaceOnce(sql, "?", username);
                sql = StringUtils.replaceOnce(sql, "?", age.toString());
                sql = StringUtils.replaceOnce(sql, "?", phone);
                sql = StringUtils.replaceOnce(sql, "?", email);
                System.out.println(sql);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        Method[] methods = UserMapper.class.getDeclaredMethods();
        for (Method method: methods) {
            if (method.isAnnotationPresent(Delete.class)){
                String sql = method.getAnnotation(Delete.class).value();
                sql = StringUtils.replaceOnce(sql,"?", String.valueOf(id));
                System.out.println(sql);
            }
        }
    }

    @Override
    public void update(String username, Integer age, String phone, String email,Integer id) {
        try {
            //2、通过Class静态方法
            Class clazz = Class.forName("com.donleo.cn.annotation.dao.UserMapper");
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method: methods) {
                if (method.isAnnotationPresent(Update.class)){
                    String sql = method.getAnnotation(Update.class).value();
                    sql = StringUtils.replaceOnce(sql, "?", username);
                    sql = StringUtils.replaceOnce(sql, "?", age.toString());
                    sql = StringUtils.replaceOnce(sql, "?", phone);
                    sql = StringUtils.replaceOnce(sql, "?", email);
                    sql = StringUtils.replaceOnce(sql,"?", String.valueOf(id));
                    System.out.println(sql);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void select() {
        try {
            //3、通过类加载器获取类对象
            ClassLoader classLoader = this.getClass().getClassLoader();
            Class clazz=classLoader.loadClass("com.donleo.cn.annotation.dao.UserMapper");
            Method[]methods = clazz.getDeclaredMethods();
            for (Method method: methods) {
                if (method.isAnnotationPresent(Select.class)){
                    String sql = method.getAnnotation(Select.class).value();
                    System.out.println(sql);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
